package com.bluemsun.naturallanguageprocessing.controller;

import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.entity.Question;
import com.bluemsun.naturallanguageprocessing.entity.UserApi;
import com.bluemsun.naturallanguageprocessing.service.IQuestionService;
import com.bluemsun.naturallanguageprocessing.util.ConductRedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping(value = "/question")
public class QuestionController {

    @Resource
    private IQuestionService questionService;

    @Resource
    private ConductRedisUtil conductRedisUtil;

    @GetMapping(value = "/get")
    public ServerResponse<Object> getHotQuestionList(@RequestParam("pageSize")Integer pageSize,@RequestParam("pageNum") Integer pageNum){
        return questionService.getHotQuestionList(pageNum,pageSize);
    }

    @GetMapping(value = "/get/{id}")
    public ServerResponse<Object> getQuestion(@PathVariable Integer id, HttpServletRequest request){
        UserApi user = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        return questionService.getQuestion(id,user.getId());
//        return questionService.getQuestion(id,1);
    }

    @GetMapping(value = "/get/classifyquestion/{classifyId}")
    public ServerResponse<Object> getClassifyQuestion(@PathVariable Integer classifyId){
        return questionService.getClassifyQuestion(classifyId);
    }

    @PostMapping(value = "/add")
    public ServerResponse<Object> addQuestion(@RequestBody Question question){
        return questionService.addQuestion(question);
    }

    @PostMapping(value = "/delete/{id}")
    public ServerResponse<Object> deleteQuestion(@PathVariable Integer id){
        return questionService.deleteQuestion(id);
    }

    @PostMapping(value = "/update")
    public ServerResponse<Object> updateQuestion(@RequestBody Question question){
        return questionService.updateQuestion(question);
    }

    @GetMapping(value = "/change")
    public ServerResponse<Object> getQuestionChange(){
        return questionService.getQuestionChange();
    }

    @GetMapping(value = "/fuzzySearch")
    public ServerResponse<Object> fuzzySearch(@RequestParam("search") String search,HttpServletRequest request){
        if (StringUtils.isEmpty(search)){
            return ServerResponse.createByErrorMessage("关键字不能为空");
        }
        return questionService.fuzzySearch(search);
    }

    @GetMapping(value = "/searchQuestions")
    public ServerResponse<Object> searchQuestions(@RequestParam("search") String search, HttpServletRequest request,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        //TODO:从redis获取用户信息
//        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
//        Integer id = userApi.getId();
        if (StringUtils.isEmpty(search)){
            return ServerResponse.createByErrorMessage("关键字不能为空");
        }
//        return questionService.fuzzySearch(search,userApi.getId());
        return questionService.searchQuestionsByTag(search,pageNum,pageSize);
    }

}
